//
//  YiJiYinChatListViewController.m
//  MyYiJianKangApp
//
//  Created by 张国文 on 16/3/30.
//  Copyright © 2016年 hangzhouyijiyin. All rights reserved.
//

#import "YiJiYinChatListViewController.h"
#import "AddPeopleAndGroupViewController.h"
#import "CreateGroupViewController.h"

@interface YiJiYinChatListViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) UIView *blackView; // 灰色背景

@end

@implementation YiJiYinChatListViewController




- (void)viewDidLoad {
    [super viewDidLoad];
    
    //设置需要显示哪些类型的会话
    [self setDisplayConversationTypes:@[@(ConversationType_PRIVATE),
                                        @(ConversationType_DISCUSSION),
                                        @(ConversationType_CHATROOM),
                                        @(ConversationType_GROUP),
                                        @(ConversationType_APPSERVICE),
                                        @(ConversationType_SYSTEM)]];
    //设置需要将哪些类型的会话在会话列表中聚合显示
    [self setCollectionConversationType:@[@(ConversationType_DISCUSSION),
                                          @(ConversationType_GROUP)]];
    [self changeNavigation];
}

// navi的修改
- (void)changeNavigation
{
    self.navigationItem.title = @"消息列表";
    UIImageView *leftImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    leftImageView.image = [UIImage imageNamed:@"houtui"];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:leftImageView];
    UITapGestureRecognizer *leftTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(lastPage:)];
    [leftImageView addGestureRecognizer:leftTapGes];
//    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"更多" style:UIBarButtonItemStylePlain target:self action:@selector(more:)];
    UIImageView *rightImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, 30, 30)];
    rightImageView.image = [UIImage imageNamed:@"baisejiaohao-xiaoxiliebiao"];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightImageView];
    UITapGestureRecognizer *rightTapGes = [[ UITapGestureRecognizer alloc] initWithTarget:self action:@selector(morePage:)];
    [rightImageView addGestureRecognizer:rightTapGes];
}

#pragma mark - 返回上一页面
- (void)lastPage:(UITapGestureRecognizer *)tapGes
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark - 更多页面
- (void)morePage:(UITapGestureRecognizer *)tapGes
{
    self.blackView = [[UIView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.blackView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    // 添加手势
    UITapGestureRecognizer *blackTapGes = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(hideTheBlackView:)];
    [self.blackView addGestureRecognizer:blackTapGes];
    UIWindow *win = [[UIApplication sharedApplication].windows lastObject];
    [win addSubview:_blackView];
    
    [self addMoreClickView];
}


#pragma mark - 隐藏灰色背景
- (void)hideTheBlackView:(UITapGestureRecognizer *)tapGes
{
    [self hideBlackView];
   
}

// 隐藏灰色背景
- (void)hideBlackView
{
    [self.blackView removeFromSuperview];
    self.blackView = nil;
}

// 建立更多的点击视图
- (void)addMoreClickView
{


    UILabel *backgroundView = [[UILabel alloc] initWithFrame:CGRectMake((kScreenWidth - 155 * kAdjustParamater), 64, 150 * kAdjustParamater, 182 * kAdjustParamater)];
    backgroundView.layer.cornerRadius = 15;
    backgroundView.layer.masksToBounds = YES;
    backgroundView.userInteractionEnabled = YES;
    backgroundView.backgroundColor = [UIColor whiteColor];
    [self.blackView addSubview:backgroundView];
    // 添加好友
    UIImageView *addFriendImage = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, 14 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    addFriendImage.image = [UIImage imageNamed:@"tianjaihaoyou-xiaoxiliebiao"];
    [backgroundView addSubview:addFriendImage];
    UIButton *addFriendButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addFriendButton.frame = CGRectMake(addFriendImage.right + 10 * kAdjustParamater, 20 * kAdjustParamater, 70 * kAdjustParamater, 20 * kAdjustParamater);
    [addFriendButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addFriendButton.titleLabel.font = [UIFont systemFontOfSize:16];

    [addFriendButton setTitle:@"添加好友" forState:UIControlStateNormal];
    [addFriendButton addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    addFriendButton.tag = 110;
    [backgroundView addSubview:addFriendButton];

    UILabel *grayLine1 = [[UILabel alloc] initWithFrame:CGRectMake(0, addFriendImage.bottom + 16 * kAdjustParamater, 150 * kAdjustParamater, 1 * kAdjustParamater)];
    grayLine1.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [backgroundView addSubview:grayLine1];
    
//    // 添加小组
    UIImageView *addGroupImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, grayLine1.bottom + 14 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    addGroupImageV.image = [UIImage imageNamed:@"tianjiaxiaozu-xiaoxiliebiao"];
    [backgroundView addSubview:addGroupImageV];
    UIButton *addGroupButton = [UIButton buttonWithType:UIButtonTypeSystem];
    addGroupButton.frame = CGRectMake(addGroupImageV.right + 10 * kAdjustParamater, grayLine1.bottom + 20 * kAdjustParamater, 70 * kAdjustParamater, 20 * kAdjustParamater);
   
    [addGroupButton setTitle:@"添加小组" forState:UIControlStateNormal];
    [addGroupButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    addGroupButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [addGroupButton addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    addGroupButton.tag = 111;
    [backgroundView addSubview:addGroupButton];
    
    UILabel *grayLine2 = [[UILabel alloc]initWithFrame:CGRectMake(0, addGroupImageV.bottom + 16 * kAdjustParamater, 150 * kAdjustParamater, 1 * kAdjustParamater)];
    grayLine2.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.1];
    [backgroundView addSubview:grayLine2];
//
    // 创建小组
    UIImageView *createGroupImageV = [[UIImageView alloc] initWithFrame:CGRectMake(20 * kAdjustParamater, grayLine2.bottom + 14 * kAdjustParamater, 30 * kAdjustParamater, 30 * kAdjustParamater)];
    createGroupImageV.image = [UIImage imageNamed:@"chuangjianxiaozu-xiaoxiliebiao"];
    [backgroundView addSubview:createGroupImageV];
    UIButton *createGroupButton = [UIButton buttonWithType:UIButtonTypeSystem];

    createGroupButton.frame = CGRectMake(createGroupImageV.right + 10 * kAdjustParamater, grayLine2.bottom + 20 * kAdjustParamater, 70 * kAdjustParamater, 20 * kAdjustParamater);
    [createGroupButton setTitle:@"创建小组" forState:UIControlStateNormal];
    [createGroupButton setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    createGroupButton.titleLabel.font = [UIFont systemFontOfSize:16];
    [createGroupButton addTarget:self action:@selector(clickChange:) forControlEvents:UIControlEventTouchUpInside];
    createGroupButton.tag = 112;
    [backgroundView addSubview:createGroupButton];
    
    // 添加动画
    backgroundView.frame = CGRectMake(backgroundView.frame.origin.x, backgroundView.frame.origin.y, backgroundView.frame.size.width, 0);
    [UIView animateWithDuration:0.3 animations:^{
        backgroundView.frame = CGRectMake(backgroundView.frame.origin.x, backgroundView.frame.origin.y, backgroundView.frame.size.width, 182 * kAdjustParamater);
    } completion:^(BOOL finished) {
        
    }];

}

#pragma mark - 点击添加好友或者添加小组或者创建小组
- (void)clickChange:(UIButton *)button
{
    switch (button.tag) {
        case 110:
        {
            NSLog(@"添加好友");
            AddPeopleAndGroupViewController *addPeopleVC = [[AddPeopleAndGroupViewController alloc] init];
            addPeopleVC.friendOrGroup = @"friend";
            [self.navigationController pushViewController:addPeopleVC animated:YES];
            break;
        }
            case 111:
        {
            NSLog(@"添加小组");
            AddPeopleAndGroupViewController *addPeopleVC = [[AddPeopleAndGroupViewController alloc] init];
            addPeopleVC.friendOrGroup = @"group";
            [self.navigationController pushViewController:addPeopleVC animated:YES];
            break;
        }
            case 112:
        {
            NSLog(@"创建小组");
            CreateGroupViewController *createGroupVC = [[CreateGroupViewController alloc] init];
            [self.navigationController pushViewController:createGroupVC animated:YES];
            break;
        }
        default:
            break;
    }
    [self hideBlackView];
}

















#pragma mark - 以下是本界面的内容
// 点击cell时触发
- (void)onSelectedTableRow:(RCConversationModelType)conversationModelType conversationModel:(RCConversationModel *)model atIndexPath:(NSIndexPath *)indexPath
{
    RCConversationViewController *conversationVC = [[RCConversationViewController alloc] init];
    conversationVC.conversationType = model.conversationType;
    conversationVC.targetId = model.targetId;
    conversationVC.title = model.conversationTitle;
    [self.navigationController pushViewController:conversationVC animated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
